package com.leyou.user.impl;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exceptions.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.auth.mapper.SpecGroupMapper;
import com.leyou.auth.mapper.SpecParamMapper;
import com.leyou.search.pojo.SpecGroup;
import com.leyou.search.pojo.SpecGroupDTO;
import com.leyou.search.pojo.SpecParam;
import com.leyou.search.pojo.SpecParamDTO;
import com.leyou.service.IgroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class groupServiceImpl implements IgroupService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    @Override
    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> select = specGroupMapper.select(specGroup);

        List<SpecGroupDTO> specGroupDTOS = BeanHelper.copyWithCollection(select, SpecGroupDTO.class);
        return specGroupDTOS;
    }

    @Override
    public List<SpecParamDTO> querySpecParams(Long gid, Long cid,Boolean searching) {
        SpecParam specParam = new SpecParam();
        List<SpecParam> select = null;
        if (gid != null) {
            specParam.setGroupId(gid);
        }
        if (cid != null) {
            specParam.setCid(cid);
        }
        if (searching!=null){
            specParam.setSearching(searching);
        }
        select = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(select)) {
            throw new LyException(ExceptionEnum.SPEC_PARAM_NOT_FOUND);
        }
        List<SpecParamDTO> specParamDTOS = BeanHelper.copyWithCollection(select, SpecParamDTO.class);
        return specParamDTOS;
    }

    @Override
    public void addGroupByGategory(SpecGroup specGroup) {
        int count = specGroupMapper.insertSelective(specGroup);
        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public void addGroupParamByGategory(SpecParam specParam) {
        int count = specParamMapper.insertSelective(specParam);
        if (count != 1) {
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    @Override
    public void deleteGroupParamByGategory(Long id) {
        int count = specParamMapper.deleteByPrimaryKey(id);
        if (count != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public void deleteGroupByGategory(Long id) {
        int count = specGroupMapper.deleteByPrimaryKey(id);
        if (count != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    @Override
    public void updateGroupByGategory(SpecGroup specGroup) {
        int count = specGroupMapper.updateByPrimaryKeySelective(specGroup);
        if (count != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public void updateGroupParamByGategory(SpecParam specParam) {
        int count = specParamMapper.updateByPrimaryKeySelective(specParam);
        if (count != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    @Override
    public List<SpecGroupDTO> querySpecsByCid(Long cid) {
        List<SpecGroupDTO> specGroupDTOS = queryGroupByCategory(cid);

        List<SpecParamDTO> specParamDTOS = querySpecParams(null,cid,null);

        Map<Long, List<SpecParamDTO>> paramsMap = specParamDTOS.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));

        specGroupDTOS.forEach(specGroupDTO -> {
            specGroupDTO.setParams(paramsMap.get(specGroupDTO.getId()));
        });

        return specGroupDTOS;
    }
}